<?php

namespace ulit;

use app\admin\model\auth\Permissions;
use app\admin\model\auth\RolePermissions;
use app\admin\model\auth\Roles;

class Tool
{

    private static Tool $instance;

    private function __construct()
    {
        // 可以在这里初始化一些资源或配置
    }

    // 私有克隆方法，防止外部克隆
    private function __clone()
    {
    }

    // 获取类的实例
    public static function getInstance():Tool
    {
        if (!isset(self::$instance)) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    function getMenuArr()
    {
        $roleId=session('role_id');
        $menuArr = include app_path() . 'config/menu.php';
        $role_info=Roles::where(['id'=>$roleId])->find();
        if($role_info['type']==1){
            return $menuArr;
        }

        $right=RolePermissions::where('role_id', $roleId)->find();
        $list=Permissions::whereIn('id',$right->right_list)->select();
        $code=[];
        foreach ($list as $k=>$v){
            foreach ($v['code'] as $k1=>$v1){
                $code[]=$v1;
            }
        }
        $controller=\request()->controller();
        $action=\request()->action();

        $checkAction=$controller.'@'.$action;
        // in_array($checkAction,$code)

        foreach ($menuArr as $k => $val) {
            foreach ($val['child'] as $s => $son) {
                if (!in_array($son['op'] . '@' . $son['act'],$code )) {
                    unset($menuArr[$k]['child'][$s]);//过滤菜单
                }
            }
        }

        return $menuArr;
    }

//    function getMenuArr()
//    {
//        $menuArr = include app_path() . 'config/menu.php';
//        $act_list = session('act_list');
//        if ($act_list != 'all' && !empty($act_list)) {
//            $right = Db::name('system_menu')->where("id in ($act_list)")->getField('right', true);
//            $role_right = '';
//            foreach ($right as $val) {
//                $role_right .= $val . ',';
//            }
//            foreach ($menuArr as $k => $val) {
//                foreach ($val['child'] as $j => $v) {
//                    foreach ($v['child'] as $s => $son) {
//                        if (strpos($role_right, $son['op'] . '@' . $son['act']) === false) {
//                            unset($menuArr[$k]['child'][$j]['child'][$s]);//过滤菜单
//                        }
//                    }
//                }
//            }
//            foreach ($menuArr as $mk => $mr) {
//                foreach ($mr['child'] as $nk => $nrr) {
//                    if (empty($nrr['child'])) {
//                        unset($menuArr[$mk]['child'][$nk]);
//                    }
//                }
//            }
//        }
//        return $menuArr;
//    }

}